﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using OpenCVDemo.Dao;
using OpenCVDemo.IService;
using OpenCVDemo.Service;
using System;

namespace OpenCVDemo.ConsoleApp
{
    class Program
    {
        static IConfigurationRoot _configuration;
        static IServiceProvider _serviceProvider;
        static Logger _logger = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            try
            {
                Init();

                //Test_EmguCV_Hello();
                //Test_EmguCV_BeginImage();
                //Test_EmguCV_VideoCapture();
                //Test_EmguCV_PracticeCase1();
                //Test_EmguCV_MatAndColor();
                //Test_EmguCV_DrawFunction();
                //Test_EmguCV_ImagePixel();
                //Test_EmguCV_ImageOperation();
                //Test_EmguCV_ContrastBrightness();
                //Test_EmguCV_ROIMask();
                //Test_EmguCV_ImageGeometricTransform();
                //Test_EmguCV_ImageFilter();
                //Test_EmguCV_ImageThreshold();
                //Test_EmguCV_DilateErode();
                //Test_EmguCV_EdgeDetection();
                //Test_EmguCV_HoughTransform();
                //Test_EmguCV_Hist();
                //Test_EmguCV_MatchTemplate();
                //Test_EmguCV_FindDrawContours();
                //Test_EmguCV_ConvexHull();
                //Test_EmguCV_ContoursBoundingRectangle();
                //Test_EmguCV_ContoursMinAreaRect();
                //Test_EmguCV_ContoursEnclosingCircle();
                //Test_EmguCV_ContoursPointPolygon();
                //Test_EmguCV_SliderVerificationCode();
                //Test_EmguCV_ContoursColorRecognition();
                //Test_EmguCV_ImageInPaint();
                //Test_EmguCV_GrabCutAndFloodFill();

                //Test_OpenCVSharp_Hello();
                //Test_OpenCVSharp_BeginImage();
                //Test_OpenCVSharp_VideoCapture();
                //Test_OpenCVSharp_PracticeCase1();
                //Test_OpenCVSharp_MatAndColor();
                //Test_OpenCVSharp_DrawFunction();
                //Test_OpenCVSharp_ImagePixel();
                //Test_OpenCVSharp_ImageOperation();
                //Test_OpenCVSharp_ContrastBrightness();
                //Test_OpenCVSharp_ROIMask();
                //Test_OpenCVSharp_ImageGeometricTransform();
                //Test_OpenCVSharp_ImageFilter();
                //Test_OpenCVSharp_ImageThreshold();
                //Test_OpenCVSharp_DilateErode();
                //Test_OpenCVSharp_EdgeDetection();
                //Test_OpenCVSharp_HoughTransform();
                //Test_OpenCVSharp_Hist();
                //Test_OpenCVSharp_MatchTemplate();
                //Test_OpenCVSharp_FindDrawContours();
                Test_OpenCVSharp_ContoursBoundingRectangle();
                //Test_OpenCVSharp_BeginPaddleOCR();

            }
            catch (Exception ex)
            {
                _logger.Error(ex, string.Format("运行Main方法时发生异常，message:{0}", ex.Message));
            }
        }

        static void Test_OpenCVSharp_BeginPaddleOCR()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.BeginPaddleOCR();
            }
        }

        static void Test_OpenCVSharp_ContoursBoundingRectangle()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ContoursBoundingRectangle();
            }
        }

        static void Test_OpenCVSharp_FindDrawContours()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.FindDrawContours();
            }
        }

        static void Test_OpenCVSharp_MatchTemplate()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.MatchTemplate();
            }
        }

        static void Test_OpenCVSharp_Hist()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.Hist();
            }
        }

        static void Test_OpenCVSharp_HoughTransform()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.HoughTransform();
            }
        }

        static void Test_OpenCVSharp_EdgeDetection()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.EdgeDetection();
            }
        }

        static void Test_OpenCVSharp_DilateErode()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.DilateErode();
            }
        }

        static void Test_OpenCVSharp_ImageThreshold()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ImageThreshold();
            }
        }

        static void Test_OpenCVSharp_ImageFilter()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ImageFilter();
            }
        }

        static void Test_OpenCVSharp_ImageGeometricTransform()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ImageGeometricTransform();
            }
        }

        static void Test_OpenCVSharp_ROIMask()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ROIMask();
            }
        }

        static void Test_OpenCVSharp_ContrastBrightness()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ContrastBrightness();
            }
        }

        static void Test_OpenCVSharp_ImageOperation()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ImageOperation();
            }
        }

        static void Test_OpenCVSharp_ImagePixel()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.ImagePixel();
            }
        }

        static void Test_OpenCVSharp_DrawFunction()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.DrawFunction();
            }
        }

        static void Test_OpenCVSharp_MatAndColor()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.MatAndColor();
            }
        }

        static void Test_OpenCVSharp_PracticeCase1()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.PracticeCase1();
            }
        }

        static void Test_OpenCVSharp_VideoCapture()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.VideoCapture();
            }
        }

        static void Test_OpenCVSharp_BeginImage()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.BeginImage();
            }
        }

        static void Test_OpenCVSharp_Hello()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var openCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                openCVService.Hello();
            }
        }

        static void Test_EmguCV_GrabCutAndFloodFill()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.GrabCutAndFloodFill();
            }
        }

        static void Test_EmguCV_ImageInPaint()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ImageInPaint();
            }
        }

        static void Test_EmguCV_ContoursColorRecognition()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ContoursColorRecognition();
            }
        }

        static void Test_EmguCV_SliderVerificationCode()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.SliderVerificationCode();
            }
        }

        static void Test_EmguCV_ContoursPointPolygon()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ContoursPointPolygon();
            }
        }

        static void Test_EmguCV_ContoursEnclosingCircle()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ContoursEnclosingCircle();
            }
        }

        static void Test_EmguCV_ContoursMinAreaRect()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ContoursMinAreaRect();
            }
        }

        static void Test_EmguCV_ContoursBoundingRectangle()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ContoursBoundingRectangle();
            }
        }

        static void Test_EmguCV_ConvexHull()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ConvexHull();
            }
        }

        static void Test_EmguCV_FindDrawContours()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.FindDrawContours();
            }
        }

        static void Test_EmguCV_MatchTemplate()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.MatchTemplate();
            }
        }

        static void Test_EmguCV_Hist()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.Hist();
            }
        }

        static void Test_EmguCV_HoughTransform()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.HoughTransform();
            }
        }

        static void Test_EmguCV_EdgeDetection()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.EdgeDetection();
            }
        }

        static void Test_EmguCV_DilateErode()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.DilateErode();
            }
        }

        static void Test_EmguCV_ImageThreshold()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ImageThreshold();
            }
        }

        static void Test_EmguCV_ImageFilter()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ImageFilter();
            }
        }

        static void Test_EmguCV_ImageGeometricTransform()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ImageGeometricTransform();
            }
        }

        static void Test_EmguCV_ROIMask()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ROIMask();
            }
        }

        static void Test_EmguCV_ContrastBrightness()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ContrastBrightness();
            }
        }

        static void Test_EmguCV_ImageOperation()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ImageOperation();
            }
        }

        static void Test_EmguCV_ImagePixel()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.ImagePixel();
            }
        }

        static void Test_EmguCV_DrawFunction()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.DrawFunction();
            }
        }

        static void Test_EmguCV_MatAndColor()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.MatAndColor();
            }
        }

        static void Test_EmguCV_PracticeCase1()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.PracticeCase1();
            }
        }

        static void Test_EmguCV_VideoCapture()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.VideoCapture();
            }
        }

        static void Test_EmguCV_BeginImage()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.BeginImage();
            }
        }

        static void Test_EmguCV_Hello()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var emguCVService = scope.ServiceProvider.GetService<IOpenCVService>();
                emguCVService.Hello();
            }
        }

        static void Init()
        {
            _configuration = CreateConfigurationRoot();
            _serviceProvider = CreateServiceProvider();
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <returns></returns>
        static IConfigurationRoot CreateConfigurationRoot()
        {
            IConfigurationBuilder builder = new ConfigurationBuilder();
            builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            return builder.Build();
        }

        /// <summary>
        /// 创建DI容器
        /// </summary>
        /// <returns></returns>
        static IServiceProvider CreateServiceProvider()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection
                .AddSingleton<IConfiguration>(_configuration)
                .AddLogging(builder =>
                {
                    builder.ClearProviders();
                    builder.AddConfiguration(_configuration.GetSection("Logging"));
                    builder.AddConsole();
                    builder.AddNLog();
                })
                .AddServices(_configuration)
                .AddDaos()
                .AddHttpClient();

            return serviceCollection.BuildServiceProvider();
        }
    }
}
